package com.qire.manhua.dal.service;

import com.qire.antsbinder.dal.DataService;
import com.qire.antsbinder.dal.annotation.StrategyAnnotation;
import com.qire.common.basic.DateUtil;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.basic.StringUtil;
import com.qire.common.constant.Url;
import com.qire.common.dal.WarehouseFactory;
import com.qire.common.model.entity.UserEntity;
import com.qire.manhua.dal.IPageDataDAL;
import com.qire.manhua.model.entity.HomeModuleEntity;
import com.qire.manhua.model.enumeration.HomeModuleType;
import com.qire.manhua.model.viewModel.impl.RecommendVipZoneViewModel;
import com.qire.manhua.model.vo.HomeRecommendVipVO;
import com.qire.manhua.model.vo.HomeRecommendSlideVO;
import com.qire.manhua.model.vo.HomeRecommendVipVO.ModuleVO;
import com.qire.manhua.model.vo.HomeRecommendVipVO.ModuleInfoVO;
import com.qire.manhua.model.vo.HomeRecommendVipVO.ModuleItemVO;

import java.util.concurrent.TimeUnit;

public class RecommendVipZoneService extends DataService {

    private IPageDataDAL pageData = WarehouseFactory.create(this, IPageDataDAL.class);

    private final RecommendVipZoneViewModel viewModel;

    public RecommendVipZoneService(RecommendVipZoneViewModel viewModel) {
        this.viewModel = viewModel;
    }

    // 更新数据
    public void loadPageData() {
        pageData.fetchRecommendVipZonePageData();
    }

    @StrategyAnnotation(event = Url.HomeRecommendVipZoneData)
    public void fetchRecommendVipZonePageDataCallBack(HomeRecommendVipVO data, boolean isCache) {

        if (data == null || data.slide == null){
            return;
        }

        if(ObjectUtil.nonNull(data.vipInfo)) {
            UserEntity.self.setVipType(data.vipInfo.vipType);
            UserEntity.self.setVipExpirationTime(data.vipInfo.expiration_time);
        }

        viewModel.clearBannerList();
        for(HomeRecommendSlideVO vo : data.slide) {
            viewModel.addVipBannerItemViewModel(vo.toAppAdvert());
        }
        viewModel.refreshBannerAdapter();

        if(ObjectUtil.nonNull(data.modules)) {
            viewModel.clearModuleList();
            for(ModuleVO vo : data.modules) {

                ModuleInfoVO infoVO = vo.module;
                HomeModuleEntity moduleEntity   = new HomeModuleEntity(infoVO.id, infoVO.name, infoVO.direction);

                for(ModuleItemVO itemVO : vo.list) {
                    int      bookId     = itemVO.book_id;
                    String   bookName   = itemVO.book_name;
                    String   title      = itemVO.title;
                    String   shortTitle = itemVO.short_title;
                    String   coverImg   = itemVO.cover_img;
                    String[] tags       = itemVO.tags;
                    int      freeNum    = itemVO.free_num;
                    String   floatTips  = "";
                    int      lastReadChapterId = 0;
                    String   lastReadChapterName = "";
                    boolean  isTeensVisible = itemVO.isTeensVisible();

                    if(StringUtil.notEmpty(itemVO.restriction_time)) {
                        if(itemVO.restriction_time.length() <= 10) {
                            String timeFormat = "0000-00-00 23:59:59";
                            itemVO.restriction_time = StringUtil.replace(itemVO.restriction_time, timeFormat, 0, timeFormat.length());
                        }
//                        int daysNumber = DateUtil.yyyy_MM_dd_HHmmss.calcDaysDifferenceToToday(itemVO.restriction_time);
                        long daysNumber = DateUtil.yMdHms.toSomeday(itemVO.restriction_time).formatBy(TimeUnit.MILLISECONDS::toDays);
                        floatTips = "限免剩余" + daysNumber + "天";
                    }

                    if(itemVO.free_num > 0) {
                        shortTitle = "限免" + itemVO.free_num + "话";
                    }

                    boolean needShowSerial = moduleEntity.homeModuleType == HomeModuleType.TYPE_RECORD;
                    if(needShowSerial) {
                        moduleEntity.addModuleItem(bookId, bookName, title, shortTitle, coverImg, tags,
                                freeNum, floatTips, lastReadChapterId, lastReadChapterName, isTeensVisible, needShowSerial);
                    } else {
                        moduleEntity.addModuleItem(bookId, bookName, title, shortTitle, coverImg, tags,
                                freeNum, floatTips, lastReadChapterId, lastReadChapterName, isTeensVisible);
                    }
                }

                viewModel.addModule(moduleEntity.buildModuleViewModels());
            }
            viewModel.refreshModuleAdapter();
        }

        viewModel.refreshUI();
    }

}
